home *** CD-ROM | disk | FTP | other *** search
- /*
- * Name: ps.rexx 6.1 (11.3.2001)
- * Description: fast tcp ports scanner
- *
- * Usage: ps <HOST> [FROM/N] [TO/N] [VER=VERBOSE/S]
- *
- * HOST - the host to scan
- * FROM - from port; 0<FROM<65536 ; default: 1
- * TO - to port; 0<FROM<=TO<65536 ; default: 80
- * VER - verbose mode
- */
-
- signal on break_c
-
- l="rmh.library";if ~show("L",l) then;if ~addlib(l,0,-30) then exit
- call MacroEnv("env","stderr")
- if AddLibrary("rexxsupport.library","rxsocket.library")~=0 then exit
-
- if ~RMH_ReadArgs("HOST/A,FROM/N,TO/N,VER=VERBOSE/S") then do
- call PrintFault()
- exit
- end
-
- if parm.1.flag then from=parm.1.value
- else from=1
-
- if parm.2.flag then to=parm.2.value
- else
- if parm.1.flag then to=from
- else to=80
- if from<1 | to>65535 | from>to then call err "bad ports sequence" from"..."to
-
- sin.addrAddr=resolve(parm.0.value)
- if sin.addrAddr=-1 then cal lerr "host <"parm.0.value"> not found"
-
- sm=2**AllocSignal()
- call SetSocketBaseSingle("SIGEVENTMASK",sm)
- ctrl_c=2**12
- ctrl_d=2**13
-
- ppt=64
- t=to-from+1
- n=t%ppt
- if (t//ppt)>0 then n=n+1
-
- if parm.3.flag then
- if IsDotAddr(parm.0.value)
- then call info "Scanning <"parm.0.value">" from"..."to "("n "cycles)"
- else call info "Scanning <"parm.0.value"> ["sin.addrAddr"]" from"..."to "("n "cycles)"
-
- do j=1 to n
-
- if (from+ppt)>to then t=to
- else t=from+ppt-1
- todo=t-from+1
-
- do i=from to t
- s=socket(inet,stream)
- if s<0 then call err "can't create socket",1
- call SetSockOpt(s,"SOCKET","EVENTMASK","CONNECT ERROR READ WRITE")
- call IOCtlSocket(s,"FIONBIO",1)
- ports.s=i
- sin.addrPort=i
- call connect(s,"SIN")
-
- srec=SetSignal(0,or(ctrl_c,sm))
- if and(srec,ctrl_c)>0 then signal break_c
- if and(srec,sm)>0 then call getEvent
- end
-
- do while todo>0
- if and(wait(or(ctrl_c,sm)),ctrl_c)>0 then signal break_c
- call getEvent
- end
-
- from=from+ppt
- end
-
- exit
-
- getEvent:
- do forever
- e=GetSocketEvents("EVENTS")
- if e<0 then return
- if events.connect then do
- string=ports.e
- if parm.3.flag then
- if GetServByPort("SERV",ports.e,"tcp") then string=serv.servName "("ports.e")"
- say string
- end
- call CloseSocket(e)
- todo=todo-1
- end
- /* never reached */
-
- info:
- parse arg msg
- call writeln("STDERR",env.prg":" msg)
- return
-
- err:
- parse arg msg,sock
- if nosock=1 then msg=msg "("errorstring()")"
- call info msg
- exit
-
- hi:
- break_c:
- call info "broken"
- exit
-
-
- /* $VER: ps.rexx 6.1 (11.3.2001) */
-